package com.sansan.qiangji.realize.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sansan.qiangji.VO.base.BaseWageAccount.BaseWageAccountListResultVO;
import com.sansan.qiangji.VO.base.BaseWageAccount.BaseWageAccountVO;
import com.sansan.qiangji.entity.base.BaseWageAccount;
import com.sansan.qiangji.entity.base.example.BaseWageAccountExample;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.BaseWageAccountAddForm;
import com.sansan.qiangji.form.base.BaseWageAccountEditForm;
import com.sansan.qiangji.form.base.BaseWageAccountListForm;
import com.sansan.qiangji.service.inter.base.BaseWageAccountService;
import com.sansan.qiangji.utils.CopyUtil;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BaseWageAccountReal {
    @Autowired
    private BaseWageAccountService baseWageAccountService;


    @Transactional
    public void deleteBaseWageAccount(Integer baseWageAccountId){
        BaseWageAccount baseWageAccount = baseWageAccountService.selectByPrimaryKey(baseWageAccountId);
        if(baseWageAccount==null){
            log.error("【工资基础删除】工资基础不存在，baseWageAccountId={}",baseWageAccountId);
            throw new ServiceException(ResultEnum.BASEWAGEACCOUNT_IS_NOT_EXIST);
        }
        int row = baseWageAccountService.deleteByPrimaryKey(baseWageAccountId);
        if(row==0){
            log.error("【工资基础删除】工资基础删除失败，baseWageAccountId={}",baseWageAccountId);
            throw new ServiceException(ResultEnum.BASEWAGEACCOUNT_DELETE_ERROR);
        }
    }
    @Transactional
    public void addBaseWageAccount(BaseWageAccountAddForm baseWageAccountAddForm){
        BaseWageAccountExample baseWageAccountExample = new BaseWageAccountExample();
        baseWageAccountExample.or().andUserNameEqualTo(baseWageAccountAddForm.getUserName());
        List<BaseWageAccount> baseWageAccountList = baseWageAccountService.selectByExample(baseWageAccountExample);
        if(baseWageAccountList.size()>0){
            log.error("【工资基础新增】该员工工资基础已存在，baseWageAccountAddForm={}",baseWageAccountAddForm.toString());
            throw new ServiceException(ResultEnum.BASEWAGEACCOUNT_IS_EXIST);
        }
        BaseWageAccount baseWageAccount = new BaseWageAccount();
        BeanUtils.copyProperties(baseWageAccountAddForm,baseWageAccount);
        int row = baseWageAccountService.insert(baseWageAccount);
        if(row==0){
            log.error("【工资基础新增】工资基础新增失败");
            throw new ServiceException(ResultEnum.BASEWAGEACCOUNT_INSERT_ERROR);
        }
    }



    @Transactional
    public void editBaseWageAccount(BaseWageAccountEditForm baseWageAccountEditForm){
        BaseWageAccount baseWageAccount = baseWageAccountService.selectByPrimaryKey(baseWageAccountEditForm.getId());
        if (baseWageAccount == null){
            log.error("【编辑物品】物品不存在，baseWageAccountEditForm={}",baseWageAccountEditForm.toString());
            throw new ServiceException(ResultEnum.BASEWAGEACCOUNT_IS_NOT_EXIST);
        }
        CopyUtil.copyPropertiesNotNull(baseWageAccountEditForm,baseWageAccount);
        int row = baseWageAccountService.updateByPrimaryKey(baseWageAccount);
        if(row==0){
            log.error("【物品新增】物品新增失败");
            throw new ServiceException(ResultEnum.BASEWAGEACCOUNT_INSERT_ERROR);
        }
    }


    public BaseWageAccountListResultVO baseWageAccountList(BaseWageAccountListForm baseWageAccountListForm){
        BaseWageAccountExample baseWageAccountExample = new BaseWageAccountExample();
        int pageNum = baseWageAccountListForm.getPageNum();
        int pageSize = baseWageAccountListForm.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        BaseWageAccountExample.Criteria criteria = baseWageAccountExample.createCriteria();
        if(baseWageAccountListForm.getUserName()!=null&&!"".equals(baseWageAccountListForm.getUserName())){
            criteria.andUserNameLike("%"+baseWageAccountListForm.getUserName()+"%");
        }
        List<BaseWageAccount> baseWageAccountList = baseWageAccountService.selectByExample(baseWageAccountExample);
        PageResult<BaseWageAccount> baseWageAccountPageResult = PageUtil.getPageResult(new PageInfo<>(baseWageAccountList));
        BaseWageAccountListResultVO baseWageAccountListResultVO = new BaseWageAccountListResultVO();
        BeanUtils.copyProperties(baseWageAccountPageResult,baseWageAccountListResultVO);
        List<BaseWageAccountVO> baseWageAccountVOS = new ArrayList<>();
        for(BaseWageAccount baseWageAccount : baseWageAccountList){
            BaseWageAccountVO baseWageAccountVO = new BaseWageAccountVO();
            BeanUtils.copyProperties(baseWageAccount,baseWageAccountVO);
            baseWageAccountVOS.add(baseWageAccountVO);
        }
        baseWageAccountListResultVO.setBaseWageAccountVOS(baseWageAccountVOS);
        return baseWageAccountListResultVO;
    }



}
